package net.qiqbframework.commandhandling;


import net.qiqbframework.messaging.annotation.ExecutableHandlerWrapper;
import net.qiqbframework.messaging.annotation.MessageExecutableHandler;
import net.qiqbframework.messaging.annotation.WrappedMessageExecutableHandler;
import net.qiqbframework.modelling.domain.AggregateCreationPolicy;

import javax.annotation.Nonnull;
import java.util.Map;


public class MethodCreationPolicyHandlingMember implements ExecutableHandlerWrapper {

    @Override
    public @Nonnull MessageExecutableHandler wrap(@Nonnull MessageExecutableHandler original) {
        return original.annotationAttributes(CreationPolicy.class)
                .map(attr -> (MessageExecutableHandler) new CreationPolicyMemberImpl(original, attr))
                .orElse(original);
    }

    private static class CreationPolicyMemberImpl extends WrappedMessageExecutableHandler
            implements CreationPolicyMember {

        private final AggregateCreationPolicy creationPolicy;

        private CreationPolicyMemberImpl(
                MessageExecutableHandler delegate, Map<String, Object> attr) {
            super(delegate);
            creationPolicy = (AggregateCreationPolicy) attr.get("creationPolicy");
        }

        @Override
        public AggregateCreationPolicy creationPolicy() {
            return creationPolicy;
        }
    }
}
